﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Bakara
{
    public partial class punto : Form
    {
        //Punto Cards
        int PA = 0;
        int PB = 0;
        int PC = 0;

        string sPA, sPB, sPC, sBA, sBB, sBC;


        //Banco Cards
        int BA = 0;
        int BB = 0;
        int BC = 0;

        //arraylist of hands which can come

        ArrayList hands = new ArrayList();
        ArrayList cards = new ArrayList();

        string current_suite;

       
        //The start up totals
        int Pt = 0;
        int Bt = 0;

        //Initiating variables for the text boxes
        string win;
        bool tie = false;
        int total_punto = 0;
        int total_banco = 0;
        int total_ties = 0;

        int iP, iB , gamec; //gamec = game counter - how many games were played

        //checking if either had to stand or not
        bool Astand = false;
        bool Bstand = false;

        //setting the random number
        Random rand = new Random();

        int cut_pack; // to cut the pack
        int cards_counter = 0; //to store the amount of cards
		
		//csv file
        static System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly();
        static string baseDir = System.IO.Path.GetDirectoryName(a.Location);
        static string exe_location = baseDir + @"\Punto_Banco_Results.csv" ;
        StreamWriter csv_file;


        //image folder
        //csv file
        static System.Reflection.Assembly img = System.Reflection.Assembly.GetEntryAssembly();
        static string img_baseDir = System.IO.Path.GetDirectoryName(img.Location);
        static string img_location = baseDir + @"\images\";
        
        
        public punto()
        {
            InitializeComponent();
        }

        private int changeNo(int numb)
        {
            if (numb >= 10)
            {
                numb = numb - 10;   
            }
            
            return numb;
        
        }
		
		private void hshow(string msg)
		{
			if (tipbox.Checked == true)
			{
				MessageBox.Show(msg);
			
            }
			return;
		}

        private bool newGame()
        {
            if (cards_counter >= cut_pack)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void cardsAfter(int position)
        {
            //removing the item
            hands.RemoveAt(iP);
            cards.RemoveAt(iP);

            cards_counter++;
        }

        private int generateNumb()
        {

            return rand.Next(0, hands.Count);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label1_Click_2(object sender, EventArgs e)
        {

        }

        private void generate_Click(object sender, EventArgs e)
        {

            //Pa.Text = "";
            //Pb.Text = "";
            PcA.ImageLocation = "";
            PcB.ImageLocation = "";
            PcC.ImageLocation = "";
            PcC.Visible = true;
            Pcl.Text = "";
            //Ba.Text = "";
            //Bb.Text = "";
            //Bc.Text = "";
            BcA.ImageLocation = "";
            BcB.ImageLocation = "";
            BcC.ImageLocation = "";
            BcC.Visible = true;
            Bcl.Text = "";
            result.Text = "";
            tiebox.Checked = false;
            Astand = false;
            Bstand = false;

            iP = 1000;
            iB = 1000;


            if (newGame() == true)
            {


                //generating 2 cards for Punto

                iP = generateNumb();
                PA = (int)hands[iP];
                //Pa.Text = cards[iP].ToString(); //writing the label

                sPA = cards[iP].ToString(); //writing the label


                PcA.ImageLocation = img_location + cards[iP].ToString();

                //removing the item
                cardsAfter(iP);


                iP = generateNumb();
                PB = (int)hands[iP];
                //Pb.Text = cards[iP].ToString();
                sPB= cards[iP].ToString(); //writing the label


                PcB.ImageLocation = img_location + cards[iP].ToString();



                Pt = PA + PB;
                Pt = changeNo(Pt); //putting to - 0 if more than 10



                hshow("Total of Punto with 2 Cards " + Pt.ToString());




                //generating 2 cards for Banco
                iB = generateNumb();
                BA = (int)hands[iB];
                //Ba.Text = cards[iB].ToString(); //writing the label

                sBA = cards[iP].ToString(); //writing the label

                BcA.ImageLocation = img_location + cards[iP].ToString();

                //removing the item
                cardsAfter(iB);


                iB = generateNumb();
                BB = (int)hands[iB];
                //Bb.Text = cards[iB].ToString();

                sBB = cards[iP].ToString(); //writing the label

                BcB.ImageLocation = img_location + cards[iP].ToString();

                //removing the item
                cardsAfter(iB);



                Bt = BA + BB;
                Bt = changeNo(Bt); //putting to - 0 if more than 10


                hshow("Total of Banco with 2 Cards " + Bt.ToString());




                //checking for natural win

                if ((Bt == 8) || (Bt == 9) || (Pt == 8) || (Pt == 9))
                {
                    //Bstand = false;

                    hshow("There is a Natural Winning Hand");

                    if (Pt == Bt)
                    {
                        win = "Tie on number " + Bt.ToString();
                        tiebox.Checked = true;
                        total_ties++;
                        tTieWins.Text = total_ties.ToString();
                        tie = true;
                    }

                    if (Pt > Bt)
                    {
                        win = "Punto - NATURAL Win with " + Pt.ToString() + " over " + Bt.ToString();
                        total_punto++;
                        tPWins.Text = total_punto.ToString();
                    }

                    else
                    {
                        win = "Banco - NATURAL Win with " + Bt.ToString() + " over " + Pt.ToString();
                        total_banco++;
                        tBWins.Text = total_banco.ToString();
                    }
                }


                else //THERE IS NO NATURAL WIN
                {


                    hshow("No Natural Win - Proceeding to 3rd Card to Punto");

                    switch (Pt)
                    {
                        case 6: Astand = true;
                            break;

                        case 7: Astand = true;
                            break;

                        case 8: Astand = true;
                            break;

                        case 9: Astand = true;
                            break;

                        default:
                            iP = generateNumb();
                            PC = (int)hands[iP];
                            //Pcl.Text = cards[iP].ToString();
                            sPC = cards[iP].ToString(); //writing the label
                            PcC.ImageLocation = img_location + cards[iP].ToString();

                            //removing the item
                            cardsAfter(iP);


                            break;
                    }


                    hshow("Game turning on the Banco now");

                    //checking if bbanco has 0-5 and if punto has no third card

                    if ((Bt >= 0) && (Bt <= 5) && (Astand == false))
                    {

                        hshow("Generating a new card to Banco since Total of Banco is less than 5 (" + Bt.ToString() + ") and Punto has a third card");

                        iB = generateNumb();
                        BC = (int)hands[iB];
                        //Bc.Text = cards[iB].ToString();
                        sBC = cards[iP].ToString(); //writing the label
                        BcC.ImageLocation = img_location + cards[iP].ToString();

                        //removing the item
                        cardsAfter(iB);


                        Bstand = false;
                    }

                    else
                    {
                        if ((Bt >= 0) && (Bt <= 2))
                        {

                            hshow("Since Banco has a total of less than 2 (" + Bt.ToString() + "), proceeding to generating another card");

                            //then generating a third card
                            iB = generateNumb();
                            BC = (int)hands[iB];
                            //Bc.Text = cards[iB].ToString();
                            sBC = cards[iP].ToString(); //writing the label
                            BcC.ImageLocation = img_location + cards[iP].ToString();

                            //removing the item
                            cardsAfter(iB);


                            Bstand = false;
                        }



                        else
                        {
                            switch (Bt)
                            {
                                case 3:
                                    if (PC != 8)
                                    {
                                        //then generating a third card

                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is NOT 8 - Generating a new Card for Banco");

                                        iB = generateNumb();
                                        BC = (int)hands[iB];
                                        //Bc.Text = cards[iB].ToString();
                                        sBC = cards[iP].ToString(); //writing the label
                                        BcC.ImageLocation = img_location + cards[iP].ToString();

                                        //removing the item
                                        cardsAfter(iB);


                                    }
                                    // else stand
                                    else
                                    {
                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is 8 - Banco Stands");
                                        Bstand = true;
                                    }
                                    break;
                                case 4:
                                    if ((PC > 1) && (PC < 8))
                                    {
                                        //then generating a third card

                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is Between 2 and 7 (included) (" + PC.ToString() + ") - Generating a new Card for Banco");

                                        iB = generateNumb();
                                        BC = (int)hands[iB];
                                        //Bc.Text = cards[iB].ToString();
                                        sBC = cards[iP].ToString(); //writing the label
                                        BcC.ImageLocation = img_location + cards[iP].ToString();

                                        //removing the item
                                        cardsAfter(iB);


                                    }
                                    // else stand
                                    else
                                    {
                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is A / 8 or above (" + PC.ToString() + ") - Banco Stands");
                                        Bstand = true;
                                    }
                                    break;
                                case 5:
                                    if ((PC >= 4) && (PC <= 7))
                                    {
                                        //then generating a third card

                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is Between 4 and 7 (included) (" + PC.ToString() + ")- Generating a new Card for Banco");

                                        iB = generateNumb();
                                        BC = (int)hands[iB];
                                        //Bc.Text = cards[iB].ToString();
                                        sBC = cards[iP].ToString(); //writing the label
                                        BcC.ImageLocation = img_location + cards[iP].ToString();

                                        //removing the item
                                        cardsAfter(iB);
                                       

                                    }
                                    // else stand
                                    else
                                    {
                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is Smaller than 4 / Bigger than 7 (" + PC.ToString() + ") - Banco Stands");
                                        Bstand = true;
                                    }
                                    break;
                                case 6:
                                    int j = PA + PB;
                                    if (((PC == 6) || (PC == 7) ||
                                        ((j == 6) || (j == 7))))
                                    {
                                        //then generating a third card

                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is Either 6 or 7 (" + PC.ToString() + ") / Or Punto's Total is Either 6 or 7 (" + j.ToString() + ")- Generating a new Card for Banco");

                                        iB = generateNumb();
                                        BC = (int)hands[iB];
                                        //Bc.Text = cards[iB].ToString();
                                        sBC = cards[iP].ToString(); //writing the label
                                        BcC.ImageLocation = img_location + cards[iP].ToString();

                                        //removing the item
                                        cardsAfter(iB);

                                    }
                                    // else stand
                                    else
                                    {
                                        hshow("Since Banco's total is (" + Bt.ToString() + ") and Punto's Third card is not 6 or 7 (" + PC.ToString() + ") - Banco Stands");
                                        Bstand = true;
                                    }
                                    break;
                                case 7:
                                    hshow("Since Banco's total is (" + Bt.ToString() + ") - Banco Stands");

                                    Bstand = true;
                                    break;
                            }


                        }


                        //Pa.Text = PA.ToString();
                        //Pb.Text = PB.ToString();

                        if (Astand == true) //if punto standing
                        {
                            PC = 0;
                            Pcl.Visible = true;
                            PcC.Visible = false;
                            Pcl.Text = "Punto Stands";
                        }


                        //Ba.Text = BA.ToString();
                       // Bb.Text = BB.ToString();


                        if (Bstand == true)
                        {
                            BC = 0;
                            Bcl.Visible = true;
                            BcC.Visible = false;
                            Bcl.Text = "Banco Stands";
                        }


                    }


                    //Displaying the results

                    //Adding the three cards
                    Bt = changeNo(BA + BB + BC);
                    Pt = changeNo(PA + PB + PC);

                    if (Bt > Pt)
                    {
                        win = "Banco - Win with " + Bt.ToString() + " over " + Pt.ToString();
                        total_banco++;
                        tBWins.Text = total_banco.ToString();
                    }

                    else if (Pt > Bt)
                    {
                        win = "Punto - Win with " + Pt.ToString() + " over " + Bt.ToString();
                        total_punto++;
                        tPWins.Text = total_punto.ToString();
                    }

                    else if (Pt == Bt) // it is a tie
                    {
                        win = "Tie on number " + Bt.ToString();
                        tiebox.Checked = true;
                        total_ties++;
                        tTieWins.Text = total_ties.ToString();
                    }


                }

                result.Text = win;


                gamec++;


                //writing to the csv file



                string ts = gamec.ToString() + "," +
                sPA.Replace(".png", "") + "," +
                sPB.Replace(".png", "") + "," +
                sPC.Replace(".png", "") + "," +
                Pt.ToString() + "," +
                sBA.Replace(".png", "") + "," +
                sBB.Replace(".png", "") + "," +
                sBC.Replace(".png", "") + "," +
                Bt.ToString() + "," +
                win;

                //MessageBox.Show(ts);
                csv_file.WriteLine(ts);
                csv_file.Flush();

            }

            else
            {
                if (MessageBox.Show("The game has reached the Cutting position (" + cut_pack.ToString() + ") of the Deck" + Environment.NewLine + "Press OK to Exit the software", "Game Reached the End", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    Application.Exit();
                }

            }



        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void punto_Load(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to create a new statistics file?", "Setting up Punto Casino Tutorial", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //creating a new csv file
                csv_file = new StreamWriter(exe_location, false);
                csv_file.WriteLine("game,PuntoA,PuntoB,PuntoC,Total_Punto,BancoA,BancoB,BancoC,Total_Banco,Wining_Result");
            }
            else
            {
                csv_file = new StreamWriter(exe_location, true);
            }

            //filling up the array list
            for (int a = 0; a < 6; a++) //iterating through 6 packs
            {     

                for (int b = 0; b < 4; b++) //iterating through the suites
                {
                    
                    switch (b)
                    {
                        case 0: current_suite = "heart-"; break;
                        
                        case 1: current_suite = "diamond-"; break;
                    
                        case 2: current_suite = "spade-"; break;

                        case 3: current_suite = "clubs-"; break;
                    }
                    
                    for (int c = 1; c < 14; c++) //iterating through the cards
                    {
                        if ((c >= 2) && (c < 10))
                        {
                            hands.Add(c);
                            cards.Add (current_suite + c.ToString() + ".png");
                        }
                        else
                        {

                            switch (c)
                            {
                                case 1: 
                                    hands.Add(c);
                                    cards.Add(current_suite + "ace.png");
                                    break;

                                case 10:
                                    hands.Add(0);
                                    cards.Add(current_suite + "10.png");
                                    break;

                                case 11:
                                    hands.Add(0);
                                    cards.Add(current_suite + "jack.png");
                                    break;

                                case 12:
                                    hands.Add(0);
                                    cards.Add(current_suite + "queen.png");
                                    break;


                                case 13:
                                    hands.Add(0);
                                    cards.Add(current_suite + "king.png");
                                    break;


                            }
                            
                            
                        }

                    }
                }
            }

            //selecting a card to divide the pack

            cut_pack = rand.Next(Convert.ToInt32(312 * 0.70), Convert.ToInt32(312 * 0.85));
            MessageBox.Show("The Deck is cut at position " + cut_pack.ToString());


        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Main Developer: Matthew Pulis (2009)" + Environment.NewLine + "Email: mpulis@matthewpulis.info", "About Me");
        }

        private void Pc_Click(object sender, EventArgs e)
        {

        }
    }
}
